﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace DevExpress.MailClient.Win.Controls.File_tabs.File_Viewers
{
    public partial class ucMapFileViewer : PRO_DOCS.BLL.WIN.Templates.DevxPermissionUserControlTemplate
    {
        public ucMapFileViewer()
        {
            InitializeComponent();
        }
        private string EXTENSION
        { get; set; }

        private string DestinationFilePath
        { get; set; }

        private string FILE_PATH
        {
            set
            {
                FileInfo docfileInfo = new FileInfo(value);
                //1. copy to a temp place
                DestinationFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), docfileInfo.Name + EXTENSION);
                if (!new FileInfo(DestinationFilePath).Exists)
                {
                    docfileInfo.CopyTo(DestinationFilePath, true);

                }
                {

                    StringBuilder sb = new StringBuilder();
                    // Read the file and display it line by line.              
                    using (System.IO.StreamReader file = new System.IO.StreamReader(DestinationFilePath))
                    {
                        //while (file.ReadLine() != null)
                        //{
                        //        // This append the text and a newline into the StringBuilder buffer       
                        //        sb.AppendLine(file.ReadLine());
                        //}
                        memoLasTextViewer.Text = file.ReadToEnd();
                    }

                }
            }
        }
        public ucMapFileViewer(string filePath, string extention)
        {
            InitializeComponent();
            EXTENSION = extention;
            FILE_PATH = filePath;
        }

        private void bbiOpenLasViewer_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {
            string surferPath = DevExpress.MailClient.Win.Properties.Settings.Default.SurferPath;
            bool pathFound = false;

            if (string.IsNullOrEmpty(surferPath))
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Title = "Select the surfer file.";
                ofd.Filter = "Executable Files | *.exe";
                if (ofd.ShowDialog().Equals(DialogResult.OK))
                {
                    DevExpress.MailClient.Win.Properties.Settings.Default.SurferPath = ofd.FileName;
                    DevExpress.MailClient.Win.Properties.Settings.Default.Save();

                    surferPath = DevExpress.MailClient.Win.Properties.Settings.Default.SurferPath;

                    pathFound = true;


                }
            }
            else
            {
                pathFound = true;
            }

            if (pathFound)
            {
                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(surferPath);
                psi.RedirectStandardOutput = true;
                psi.Arguments = DestinationFilePath;
                // psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                psi.UseShellExecute = false;
                System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
                System.IO.StreamReader myOutput = proc.StandardOutput;
            }

        }
    }
}
